package com.example.mqttledmenu;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import org.eclipse.paho.android.service.MqttAndroidClient;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ViewPager2 viewPager;
    private LinearLayout tab_launch, tab_mine;
    private ImageView iv_launch, iv_mine, iv_current;

    //权限获取函数
    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Log.i("permission", "获取权限成功!");
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 201);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();
        initPager();
        initTabView();
        initTabbarListener();
    }


    public void initTabbarListener() {
        final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main));
        softKeyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
            @Override
            public void onSoftKeyboardOpened(int keyboardHeightInPx) {
                //键盘打开
                //Log.d("nightowl", "键盘打开");
                findViewById(R.id.table_bar).setVisibility(View.INVISIBLE);
            }

            @Override
            public void onSoftKeyboardClosed() {
                //键盘关闭
                // Log.d("nightowl", "键盘关闭");
                findViewById(R.id.table_bar).setVisibility(View.VISIBLE);
            }
        });
    }

    public void initTabView() {
        tab_launch = findViewById(R.id.tab_launch);
        tab_mine = findViewById(R.id.tab_mine);

        tab_launch.setOnClickListener(this);
        tab_mine.setOnClickListener(this);
        iv_launch = findViewById(R.id.iv_launch);
        iv_mine = findViewById(R.id.iv_mine);
        iv_launch.setOnClickListener(this);
        iv_mine.setOnClickListener(this);

        iv_launch.setSelected(true);
        iv_current = iv_launch;
    }

    private void changeTab(int position) {
        iv_current.setSelected(false);
        switch (position) {
            case 0:
                iv_launch.setSelected(true);
                iv_current = iv_launch;
                break;
            case 1:
                iv_mine.setSelected(true);
                iv_current = iv_mine;
                break;
        }
    }

    public void initPager() {
        viewPager = findViewById(R.id.viewPager);
        Toast.makeText(this, "欢迎回来!", Toast.LENGTH_SHORT).show();
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new FragmentLaunch());
        fragments.add(new FragmentMine());
        FragmentPagerAdapter PagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        viewPager.setAdapter(PagerAdapter);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tab_launch:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tab_mine:
                viewPager.setCurrentItem(1);
                break;
        }
    }
}
